package com.lkz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkz.entity.Permission;
import com.lkz.service.IPermissionService;
import com.lkz.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  前端控制器
 */
@RestController
@RequestMapping("/emp")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;
    @GetMapping("/perm")
    public List<Permission> selectPerm(){
        return permissionService.list();
    }
    @GetMapping("/perm/list")
    public Map getList(Integer page, Integer limit){
        System.out.println("进入");
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Permission> pages = new Page<Permission>();
        pages.setSize(limit);
        pages.setCurrent(page);
        IPage<Permission> iPage = permissionService.selectList(pages);
        map.put("msg","");
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);
        return map;
    }
    @GetMapping("/perm/selectByDel")
    public Map selectByDel(Integer page, Integer limit){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Permission> pages = new Page<Permission>();
        pages.setSize(limit);
        pages.setCurrent(page);
        IPage<Permission> iPage = permissionService.selectByDel(pages);
        map.put("msg","");
        map.put("count",iPage.getTotal());
        map.put("data",iPage.getRecords());
        map.put("code",0);
        return map;
    }
    @DeleteMapping("/perm/del/{permId}")
    public Map del(@PathVariable Integer permId){
        Map<String,Object> result = new HashMap<String,Object>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("perm_id",permId);
        Permission permission = new Permission();
        permission.setIsDel(1);
        result.put("state",permissionService.update(permission,queryWrapper));
        return result;
    }
    @DeleteMapping("/perm/recover/{permId}")
    public Map recover(@PathVariable Integer permId){
        Map<String,Object> result = new HashMap<String,Object>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("perm_id",permId);
        Permission permission = new Permission();
        permission.setIsDel(0);
        result.put("state",permissionService.update(permission,queryWrapper));
        return result;
    }
    @PostMapping("/perm")
    public Map add(Permission permission){
        Map<String,Object> result = new HashMap<String,Object>();
        permission.setCreateTime(StringUtils.getNowTime());
        permission.setUpdateTime(permission.getCreateTime());
        permission.setIsDel(0);
        result.put("state",permissionService.save(permission));
        return result;
    }
    @PutMapping("/perm")
    public Map edit(Permission permission){
        Map<String,Object> result = new HashMap<String,Object>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("perm_id",permission.getPermId());
        permission.setUpdateTime(StringUtils.getNowTime());
        result.put("state",permissionService.update(permission,queryWrapper));
        return result;
    }
}
